package com.ververica.cdc.guass.sink.jdbc.testutils;

import com.ververica.cdc.guass.sink.jdbc.JdbcConnectionOptions;
import org.apache.flink.util.FlinkRuntimeException;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;

/** Describes a database: driver, schema and urls. */
public interface DatabaseMetadata extends Serializable {

    String getJdbcUrl();

    String getJdbcUrlWithCredentials();

    String getUsername();

    String getPassword();

/*    XADataSource buildXaDataSource();*/

    String getDriverClass();

    String getVersion();

/*    default SerializableSupplier<XADataSource> getXaSourceSupplier() {
        return this::buildXaDataSource;
    }*/

    default JdbcConnectionOptions getConnectionOptions() {
        return new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                .withDriverName(getDriverClass())
                .withUrl(getJdbcUrl())
                .withUsername(getUsername())
                .withPassword(getPassword())
                .build();
    }

    default Connection getConnection() {
        try {
            Class.forName(getDriverClass());
            return DriverManager.getConnection(getJdbcUrl(), getUsername(), getPassword());
        } catch (Exception e) {
            throw new FlinkRuntimeException(e);
        }
    }
}
